//
//  LiveDetailUiParameter.m
//  DuTe
//
//  Created by Gikki Ares on 2020/12/18.
//  Copyright © 2020 vgemv. All rights reserved.
//

#import "JsBottomInputBarVm.h"
#import <JsUtilitySdk/JsGeometryManager.h>

@interface JsBottomInputBarVm()

/*TextView Ui Parameter Start*/
@property(nonatomic,assign) float textViewTopSpacing;
@property(nonatomic,assign) float textViewBottomSpacing;
@property(nonatomic,assign) float textViewLeadingSpacing;
@property(nonatomic,assign) float textView_btn_spacing;
/*TextView Ui Parameter End*/

	
/* Content Ui Parameter Start*/
@property(nonatomic,assign) float btnTrailingSpacing;
@property(nonatomic,assign) float btn_btn_spacing;
/* Content Ui Parameter End*/

@property(nonatomic,assign)CGFloat sendBtnWidth;
@property(nonatomic,assign)CGFloat sendBtnHeight;

@property(nonatomic,assign)CGFloat emptyHeight;

	//声音按钮大小
//voiceInputViewHeight
@property(nonatomic,assign) CGFloat voiceInputViewHeight;

@end

@implementation JsBottomInputBarVm

//static float smallPadding = 5;
static CGRect textViewInitFrame;

+ (void)load {
	textViewInitFrame = CGRectMake(10, 5, 50, 40);
}


+(JsBottomInputBarVm *)sharedInstance {
	static dispatch_once_t onceToken;
	static JsBottomInputBarVm * _sharedInstance;
	
	dispatch_once(&onceToken, ^{
		_sharedInstance = [[JsBottomInputBarVm alloc] init];
	});
	return _sharedInstance;
}

//vm初始化时,计算好view的各个控件的初始frame.
//刚init时不计算,因为此时frame为0.再设置了frame时再更新各个控件的frame.
- (instancetype)init {
	if(self = [super init]) {
		
		
		
		//输入框的高度
//		self.mf_barHeight = 50;
		//输入框的宽度
		self.mf_barWidth = [[UIScreen mainScreen] bounds].size.width;

		
		self.mf_expressionWidth = 41;
		self.mf_expressionHeight = 41;
		
		self.mf_voiceHeight = self.mf_expressionHeight;
		self.mf_voiceWidth = self.mf_expressionWidth;
		
		self.sendBtnHeight = self.mf_expressionHeight;
		self.sendBtnWidth = self.mf_expressionWidth;

		/* TextView Ui Parameter Start*/
		self.textViewTopSpacing = 10;
		self.textViewBottomSpacing = 10;
		self.textViewMaxHeight = 100;
		self.textViewLeadingSpacing = 20;
		self.textView_btn_spacing = 5;
		self.textViewVm.maxHeight = 100;
		/* TextView Ui Parameter End*/
		
		
		/* Content Ui Parameter Start*/
		self.btnTrailingSpacing = 20;
		self.btn_btn_spacing = 5;
		/* Content Ui Parameter End*/
		
		
		self.expressionViewSize = CGSizeMake(JsGeometryManager.screenWidth, 300);
		
		
		self.textViewVm = [[JsTextViewVm alloc]init];
		
		
		self.voiceInputViewHeight = 300;
		self.normalBackgroundColor = [UIColor blackColor];
		self.expanedBackgroundColor = [UIColor greenColor];
		
		//刚初始化就计算
		[self doCalculate];
	}
	return self;
}

- (void)setSize:(CGSize)size {
	_size = size;
	[self doCalculate];
}

//context更新,需要重新计算.不手动触发,自己调用.
- (void)setContent:(NSString *)content {
	_content = content;
	
	self.textViewVm.content = content;
}

- (void)setPlaceholder:(NSString *)placeholder {
	_placeholder = placeholder;
	self.textViewVm.placeholder = placeholder;
}



- (BOOL)isEmpty {
	if(self.content.length == 0) {
		return YES;
	}
	else {
		return NO;
	}
}

//计算当前bar的Size
//首先计算子视图的size,再计算父视图的size
- (void)doCalculate {
	
	
	/* TextView Ui Parameter Start*/
	//这个是关键计算参数,
	CGFloat textViewWidth = 0;
	
	//没有展开且内容不为空显示两个按钮,其余都显示3个按钮.
	textViewWidth = JsGeometryManager.screenWidth - self.textViewLeadingSpacing - self.textView_btn_spacing - self.mf_expressionWidth - self.mf_voiceWidth - self.sendBtnWidth - self.btnTrailingSpacing - self.btn_btn_spacing;
	if(self.isExpaneded) {
		//expanded
		self.textViewVm.contentColor = [UIColor blackColor];
	}
	else {
		//not expanded
		self.textViewVm.contentColor = [UIColor whiteColor];
		
		if(self.isEmpty) {
			//没有展开且内容不为空显示两个按钮,其余都显示3个按钮.
			textViewWidth = JsGeometryManager.screenWidth - self.textViewLeadingSpacing - self.textView_btn_spacing - - self.mf_expressionWidth - self.mf_voiceWidth  - self.btnTrailingSpacing - self.btn_btn_spacing;
		}
	}

	//textView计算前必须提供宽度.
	self.textViewVm.initalWidth = textViewWidth;
	[self.textViewVm doCalculate];
	/* TextView Ui Parameter End*/
	
	
	// computedHeight
	float computedHeight = _textViewVm.computedSize.height + _textViewTopSpacing + _textViewBottomSpacing;
	self.emptyHeight = _textViewVm.emptyHeight + _textViewTopSpacing + _textViewBottomSpacing;
	
	if(self.isEmpty && !self.isExpaneded) {
		_sendBtnFrame = CGRectMake(JsGeometryManager.screenWidth, 0, 0, 0);
	}
	else {
		//send
		CGFloat x = JsGeometryManager.screenWidth - self.btnTrailingSpacing - self.sendBtnWidth;
			//不应该居中显示,而是根据
		CGFloat y = (self.emptyHeight - self.mf_expressionHeight) / 2;
		CGFloat w = self.sendBtnWidth;
		CGFloat h = self.sendBtnHeight;
		_sendBtnFrame = CGRectMake(x, y, w, h);
	}
	
	//expression
	CGFloat x = [JsGeometryManager xLeftFrame:_sendBtnFrame width:self.mf_expressionWidth offset:-self.btn_btn_spacing];
	//不应该居中显示,而是根据
	CGFloat y = (self.emptyHeight - self.mf_expressionHeight) / 2;
	CGFloat w = self.mf_expressionWidth;
	CGFloat h = self.mf_expressionHeight;
	_expressionBtnFrame = CGRectMake(x, y, w, h);
	
	//voice
	x = [JsGeometryManager xLeftFrame:_expressionBtnFrame width:self.mf_voiceWidth offset:-self.btn_btn_spacing];
	 y = (self.emptyHeight - self.mf_voiceHeight) / 2;
	w = self.mf_voiceWidth;
	 h = self.mf_voiceHeight;
	_voiceBtnFrame =  CGRectMake(x, y , w, h);
	
	//textView,这个textViewFrame的高度不应该根据self的frame来计算,而应该根据内容来计算
	x = self.textViewLeadingSpacing;
	y = self.textViewTopSpacing;
	w = _voiceBtnFrame.origin.x - self.textViewLeadingSpacing -self.textView_btn_spacing;
	h = _textViewVm.computedSize.height;
	//textView的size +
	
	_textViewFrame = CGRectMake(x, y, w, h);
	
	if(self.isExpaneded) {
		self.textViewVm.backgroundColor = [UIColor lightGrayColor];
//		self.backgroundColor = [UIColor whiteColor];

		//如果expression没有显示,但是还是需要计算的.
		self.headerHeight = computedHeight;
		
		
		self.expressionViewFrame = CGRectMake(0, self.headerHeight, JsGeometryManager.screenWidth, self.expressionViewSize.height);
		
		self.voiceInputViewFrame = CGRectMake(0, self.headerHeight, JsGeometryManager.screenWidth, self.voiceInputViewHeight);
	
		
		//bottomBar的高度只和文字有关系
		if(self.isExpressionDisplay) {
			computedHeight += self.expressionViewSize.height;
		}
		else if(self.isVoiceDisplay){
			computedHeight += self.voiceInputViewHeight;
		}
		else if(self.isKeyboardDisplay) {
			//键盘
//			computedHeight += self.voiceInputViewHeight;
		}
		
	}
	else {
		self.textViewVm.backgroundColor = [UIColor darkGrayColor];
//		self.backgroundColor = [UIColor blackColor];
	}
	
	self.computedSize = CGSizeMake(JsGeometryManager.screenWidth, computedHeight);
	NSLog(@"BottomBar size is %@",NSStringFromCGSize(self.computedSize));
}

+ (float) btnImageSize {
	return 27;
}


@end
